<?php
/*
** Business类 商户功能函数
*/
include_once("./mysql.php");
class Bus extends Database{
	/*
	||	login 登录函数
	||	参数：[string:account,string:password]
	*/
	function login($arr){
		include_once("./Security.php");		
		$account = $arr['account'];
		$password = comHash($arr['password']);
        $con = self::open();
		if($con){
			$sqlString = "select bid,name from businessaccount where account = '".$account."' and password = '".$password."'";
			$re = self::$conn->query($sqlString);
			if($re==false){
				echo "Some Error in query";
				return;
			}
			$arr = mysqli_fetch_assoc($re);
			if($arr!=false){
				echo json_encode($arr,JSON_UNESCAPED_UNICODE);
			}else{
				echo "0";
			}
		}else{
			echo 'sqlError';
		}
		self::__destruct();
	}
	/*
	||	registe 注册函数
	||	参数：[string:account,string:password]
	*/	
	function registe($arr){
		include_once("./Security.php");	
		$account = $arr['account'];
		$password = comHash($arr['password']);
        $con = self::open();
		if($con){
			//先检查是否有相同的用户名
			$sqlSearchString = "select bid from businessAccount where account = '".$account."'";
			$re = self::$conn->query($sqlSearchString);
			if($re==false){
				echo "Some Error in query";
				return;
			}			
			$bidArr = mysqli_fetch_assoc($re);
			//如果没有再注册
			if($bidArr==false){
				$re = self::$conn->query("insert into businessAccount (account,password,name) values ('".$account."','".$password."','未审核商家')");
				echo "1";		
			}else{
				//用户名重复，返回null通知
				echo "0";
			}

		}else{
			echo 'sqlError';
		}
		self::__destruct();
	}
	/*
	||	checkGoods 查看商品
	||	参数：[int:bid]
	*/
	function checkGoods($arr){
		$bid = $arr['bid'];
		$con = self::open();
		if($con){
			$sqlString = "select * from goods where bid = ".$bid;
			$re = self::$conn->query($sqlString);
			$arrgoods= array();
			while(true){
				$arr=mysqli_fetch_assoc($re);
				if($arr==false){
					break;
				}
				array_push($arrgoods,$arr);
			}
			echo json_encode($arrgoods,JSON_UNESCAPED_UNICODE);
		}else{
			echo '{
			"re":"sqlErr"
			}';
		}
		self::__destruct();
	}
	/*
	||	addGood 添加商品
	||	参数：
	||	[int:bid,string:gtype,string:name,string:type,
	||  		string:specifications,string:price,string:acttime,
	||  		string:author,string:description],
	||	string:host_address
	*/
	function addGood($arr,$host_address){
		$newfolderPath = "../img/goods/".$arr['bid'];
		//先将商品图片放好
		if(!file_exists($newfolderPath)) mkdir($newfolderPath);
		$timeStep = time();
		$fname0 = $timeStep."-smallPic.jpg";
		$fname1 = $timeStep."-longPic.jpg";
		$fpath0 = "../img/goods/".$arr['bid']."/".$fname0;
		$fpath1 = "../img/goods/".$arr['bid']."/".$fname1;
		move_uploaded_file($_FILES['files']['tmp_name'][0],$fpath0);
		move_uploaded_file($_FILES['files']['tmp_name'][1],$fpath1);
		//生成商品图片路径
		$fileaddress0 = "http://".$host_address."/gallaryMarket/img/goods/".$arr['bid']."/".$fname0;
		$fileaddress1 = "http://".$host_address."/gallaryMarket/img/goods/".$arr['bid']."/".$fname1;
		//然后将商品数据插入数据库
		$con = self::open();
		if($con){
			$sqlString1 = "insert into goods (bid,gtype,name,type,specifications,price,acttime,author,smallPicUrl,description,longPicUrl)values (".$arr['bid'].",'".$arr['gtype']."','".$arr['name']."','".$arr['type']."','".$arr['specifications']."','".$arr['price']."','".$arr['acttime']."','".$arr['author']."','".$fileaddress0."','".$arr['description']."','".$fileaddress1."')";
			$sqlString2 = "select * from goods where bid = ".$arr['bid']." ORDER BY gid DESC LIMIT 1;";	
			$re1 = self::$conn->query($sqlString1);
			//查询刚添加的商品数据然后返回(同时也可验证商品添加是否成功)
			if($re1!=false) $re2 = self::$conn->query($sqlString2);
			if($re2!=false)	$result = mysqli_fetch_assoc($re2);
			echo json_encode($result,JSON_UNESCAPED_UNICODE);
		}else{
			echo '{
			"re":"sqlErr"
			}';
		}
		self::__destruct();
	}
	/*
	||	deleteGood 删除商品
	||	参数：[int:gid]
	*/
	function deleteGood($arr){
		$gidList = $arr['gidList'];
		$bid = $arr['bid'];
		$con = self::open();
		if($con){
			//先删除商品相关图片和评论
			foreach (explode(' ',$gidList) as $gid) {
				$re = self::$conn->query("select smallPicUrl,longPicUrl from goods where gid = ".$gid);
				if($re!=false){
					$picUrlArr = mysqli_fetch_assoc($re);
					$tmpArr = explode('/',$picUrlArr['smallPicUrl']);
					$filename0 = $tmpArr[count($tmpArr)-1];
					$tmpArr = explode('/',$picUrlArr['longPicUrl']);
					$filename1 = $tmpArr[count($tmpArr)-1];
					unlink("../img/goods/".$bid."/".$filename0);
					unlink("../img/goods/".$bid."/".$filename1);	
				}
				//删除评论代码...
			}
			//再删除商品数据
			$sqlString="delete from goods where bid = ".$bid." and gid in (".implode(',', explode(' ', $gidList)).")";
			$re = self::$conn->query($sqlString);
			if($re!=false) echo "delete ok";
		}else{
			echo '{
			"re":"sqlErr"
			}';
		}
		self::__destruct();
	}
	/*
	||	checkOrder 查看订单
	||	参数：[int:bid]
	*/
	function checkOrders($arr){
		$bid = $arr['bid'];
		$con = self::open();
		if($con){
			$sqlString = "CALL BusCheckOrderProcedure(".$bid.")";
			$re = self::$conn->query($sqlString);
			$arrOrders= array();
			while(true){
				$arr=mysqli_fetch_assoc($re);
				if($arr==false){
					break;
				}
				array_push($arrOrders,$arr);
			}
			echo json_encode($arrOrders,JSON_UNESCAPED_UNICODE);
		}else{
			echo '{
			"re":"sqlErr"
			}';
		}
		self::__destruct();
	}
	/*
	||	dealOrder 处理订单
	||	参数：[int:bid,int:oid]
	*/
	function dealOrder($arr){
		$bid = $arr['bid'];
		$oid = $arr['oid'];
		$con = self::open();
		if($con){
			$sqlString = "CALL BusOfferProcedure(".$oid.",".$bid.")";
			$re = self::$conn->query($sqlString);
			if($re==false) echo "query error";
		}else{
			echo '{
			"re":"sqlErr"
			}';
		}
		self::__destruct();		
	}
}

?>